package com.kystar.muxplayer.selfview;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;

import com.kystar.muxplayer.R;
import com.kystar.muxplayer.base.TDevice;


/**
 * Created by peanut on 2018/8/16
 */
public class MyKeyBoard implements View.OnClickListener {
    PopupWindow mPopupWindow;
    MyEditText mEditText;
    MyConstraintLayout mConStraintLayout;

    public MyKeyBoard(Context context, MyConstraintLayout myConStraintLayout) {
        mConStraintLayout = myConStraintLayout;
        View contentView = LayoutInflater.from(context).inflate(R.layout.popupwindow_keyboard, null);
        mPopupWindow = new PopupWindow(contentView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, false);
        mPopupWindow.setContentView(contentView);
        contentView.findViewById(R.id.num_1).setOnClickListener(this);
        contentView.findViewById(R.id.num_2).setOnClickListener(this);
        contentView.findViewById(R.id.num_3).setOnClickListener(this);
        contentView.findViewById(R.id.num_4).setOnClickListener(this);
        contentView.findViewById(R.id.num_5).setOnClickListener(this);
        contentView.findViewById(R.id.num_6).setOnClickListener(this);
        contentView.findViewById(R.id.num_7).setOnClickListener(this);
        contentView.findViewById(R.id.num_8).setOnClickListener(this);
        contentView.findViewById(R.id.num_9).setOnClickListener(this);
        contentView.findViewById(R.id.num_back).setOnClickListener(this);
        contentView.findViewById(R.id.num_0).setOnClickListener(this);
        contentView.findViewById(R.id.num_clear).setOnClickListener(this);
       // mPopupWindow.setFocusable(true);
        mPopupWindow.setBackgroundDrawable(new BitmapDrawable((Bitmap) null));
    }

    public void show(MyEditText editText) {
        mEditText = editText;
        mConStraintLayout.setEditText(editText);
        mConStraintLayout.setPopupWindow(mPopupWindow);
        int offX = (editText.getWidth() / 2 - Math.round(TDevice.dpToPixel((float) 60.0) / 2));
        if (!mPopupWindow.isShowing()) {
            mPopupWindow.showAsDropDown(editText,offX,0);
        }
    }

    @Override
    public void onClick(View v) {
        String string;
        int index = mEditText.getSelectionStart();
        if (mEditText.getText() != null) {
            string = mEditText.getText().toString();
        } else {
            string = "";
        }
        StringBuffer sb = new StringBuffer(string);
        index = sb.length();
        switch (v.getId()) {
            case R.id.num_1:
                sb.insert(index, "1");
                index+=1;
                break;
            case R.id.num_2:
                sb.insert(index, "2");
                index+=1;
                break;
            case R.id.num_3:
                sb.insert(index, "3");
                index+=1;
                break;
            case R.id.num_4:
                sb.insert(index, "4");
                index+=1;
                break;
            case R.id.num_5:
                sb.insert(index, "5");
                index+=1;
                break;
            case R.id.num_6:
                sb.insert(index, "6");
                index+=1;
                break;
            case R.id.num_7:
                sb.insert(index, "7");
                index+=1;
                break;
            case R.id.num_8:
                sb.insert(index, "8");
                index+=1;
                break;
            case R.id.num_9:
                sb.insert(index, "9");
                index+=1;
                break;
            case R.id.num_0:
                sb.insert(index, "0");
                index+=1;
                break;
            case R.id.num_back:
                if (index != 0) {
                    sb.replace(index - 1, index, "");
                    index-=1;
                }
                break;
            case R.id.num_clear:
                sb = new StringBuffer("");
                index =0;
                break;
        }
        if(sb.length()>10) return;
        mEditText.setText(sb);
    //    mEditText.setSelection(index);
    }


}
